var urlcatalog = 'http://localhost:8080/service/Catalog.groovy';
var $j = jQuery.noConflict();

function setOrUnsetDisplay(id){
	var asd = document.getElementById(id);
	var toChange = asd.lastChild;
	if(toChange.style.display=='none'){
		toChange.style.display='';
	}else{
		toChange.style.display='none';
	}
	
}

function getCategory(languajeId) {
	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				parameters : {
					method : 'GetCategoryList',
					language_id : languajeId
				},
				onSuccess : function(transport) {
					var response = transport.responseXML || "no response text";
					var categories = response.getElementsByTagName("category");
					//document.getElementById("mainContent").innerHTML="<div id=\"productsContent\"><div id=\"products\"></div></div>";
					// alert('aca');
				var productsDiv = document.getElementById("categories");
				for ( var i = 0; i < categories.length; i++) {
					// alert(categories[i].getElementsByTagName("name")[0].firstChild.nodeValue);
				var divi = document.createElement("div");
				// var attr = document.createAttibute("id");
				divi.setAttribute("id", "category" + (i + 1));
				//divi.setAttribute("onclick","alert(\""+categories[i].getElementsByTagName("name")[0].firstChild.nodeValue+"\");");
				
				divi.setAttribute("code", categories[i]
						.getElementsByTagName("code")[0].firstChild.nodeValue);
				//var name = document.createElement("a");
				var name = document.createElement("div");

				name.setAttribute("class", "categoryName");
			//	name.setAttribute("href", "#");
				name.setAttribute("onclick","setOrUnsetDisplay( \""+ divi.getAttribute("id") +"\" ); GetProductListByCategory("+ (i + 1) + "," + languajeId + ");");
				name.innerHTML = categories[i].getElementsByTagName("name")[0].firstChild.nodeValue;
				divi.appendChild(name);
				var subcat = document.createElement("div");
				// divi.appendChild(subcat);
				
				setSubcategoryList(divi, i + 1, languajeId);
				// divi.setAttribute("onclick","getSubcategoryListAsTable(" + (i
				// + 1) +","+ languajeId +")");
				// productsDiv.innerHTML = productsDiv.innerHTML + '<p>' +
				// categories[i].getElementsByTagName("name")[0].firstChild.nodeValue
				// + '</p>'
				productsDiv.appendChild(divi);

			}
		},
		onFailure : function() {
			alert('Problems on Ajax Request')
		}

			});
}
/*
function getProductList(crit) {
	var req = new Ajax.Request(urlcatalog, {
		method : 'get',
		parameters : {
			method : 'GetProductListByName',
			criteria : 'terven'
		},
		onSuccess : function(transport) {
			var response = transport.responseText || "no response text";
			alert(response);
		},
		onFailure : function() {
			alert('Problems on Ajax Request')
		}

	});
}*/

function setSubcategoryList(divit, category, lenguage) {
	/* alert(divit) */
	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				parameters : {
					method : 'GetSubcategoryList',
					category_id : category,
					language_id : lenguage
				},
				onSuccess : function(transport) {
					var response = transport.responseXML || "no response text";
					var subcategories = response
							.getElementsByTagName("subcategory");
//					var divsubcat = document.createElement("div");
					var divsubcat = document.createElement("div");

					var ul = document.createElement("ul");
					
					divsubcat.setAttribute("id", "subcategorias");
					for ( var i = 0; i < subcategories.length; i++) {
						var li = document.createElement("li");
						var divi = document.createElement("a");

						//divi.setAttribute("id", "subcategory" + (i + 1));
						
						divi.setAttribute("id", "subcategory" + subcategories[i].getAttribute("id"));
						divi.setAttribute("code",subcategories[i].getElementsByTagName("code")[0].firstChild.nodeValue);
						divi.setAttribute("category_id",subcategories[i].getElementsByTagName("category_id")[0].firstChild.nodeValue);
						li.setAttribute("class", "subcategory");
						divi.innerHTML = subcategories[i].getElementsByTagName("name")[0].firstChild.nodeValue
						divi.setAttribute("onclick","GetProductListBySubcategory(" +
										category + "," + 
										//subcategories[i].getElementsByTagName("name")[0].firstChild.nodeValue + "," +
										subcategories[i].getAttribute("id") + "," +
										lenguage + ");");
						// alert(divi.innerHTML);
						// productsDiv.innerHTML = productsDiv.innerHTML + '<p>'
						// +
						// subcategories[i].getElementsByTagName("name")[0].firstChild.nodeValue
						// + '</p>'
						li.appendChild(divi);
						ul.appendChild(li);

					}
					divsubcat.appendChild(ul);
					$(divsubcat).setStyle( {
						display: 'none'
					});

					divit.appendChild(divsubcat);
					// alert('response \n' + response);
					// return response;
					// alert(response);
				},
				onFailure : function() {
					alert('Problems on Ajax Request')
				}

			});
	return req;
}
/*
 * function getSubcategoryListAsTable(category, lenguage) { var subCatList =
 * getSubcategoryList(category, lenguage).onSuccess; alert(subCatList); }
 */

function GetProductListByCategory(category, languajeId) {
	// var productsdiv = document.createElement("div");

	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				parameters : {
					method : 'GetProductListByCategory',
					category_id : category,
					language_id : languajeId
				},
				onSuccess : function(transport) {

					var response = transport.responseXML || "no response text";
					var products = response.getElementsByTagName("product");
					// alert('aca');
					document.getElementById("mainContent").innerHTML="<div id=\"productsContent\"><div id=\"products\"></div></div>";
					var productsDiv = document.getElementById("products");
					productsDiv.innerHTML="";
					for ( var i = 0; i < products.length; i++) {

						var divi = document.createElement("div");
						// var attr = document.createAttibute("id");
						divi.setAttribute("class","productDiv");
						divi.setAttribute("id", "product"
								+ products[i].getAttribute("id"));
						divi.setAttribute("category_id",products[i].getElementsByTagName("category_id")[0].firstChild.nodeValue);
						divi.setAttribute("subcategory_id",products[i].getElementsByTagName("subcategory_id")[0].firstChild.nodeValue);
						divi.setAttribute("sales_rank",products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue);
						divi.setAttribute("price",products[i].getElementsByTagName("price")[0].firstChild.nodeValue);
						var imgDiv = document.createElement("div");
						imgDiv.setAttribute("class", "ProductImg");
						var image = document.createElement("img");
						image.setAttribute("src",products[i].getElementsByTagName("image_url")[0].firstChild.nodeValue);
						imgDiv.appendChild(image);
						var name = document.createElement("div");
						name.setAttribute("class", "ProductName");
						name.innerHTML = "<p>" + products[i]
								.getElementsByTagName("name")[0].firstChild.nodeValue+"</p>";
						name.setAttribute("onclick","GetProduct(" + products[i].getAttribute("id") + ");");
						var price = document.createElement("div");
						price.innerHTML = "<p> $ " + divi.getAttribute("price") +"</p>";
	
						divi.appendChild(name);
						divi.appendChild(price);
						divi.appendChild(imgDiv);

						// alert(products[i].getElementsByTagName("name")[0].firstChild.nodeValue)

						// var subcat = document.createElement("div");
						// divi.appendChild(subcat);
						// setSubcategoryList(divi,i+1,languajeId);
						// divi.setAttribute("onclick","getSubcategoryListAsTable("
						// + (i + 1) +","+ languajeId +")");
						// divi.innerHTML =
						// categories[i].getElementsByTagName("name")[0].firstChild.nodeValue
						// productsDiv.innerHTML = productsDiv.innerHTML + '<p>'
						// +
						// categories[i].getElementsByTagName("name")[0].firstChild.nodeValue
						// + '</p>'
						productsDiv.appendChild(divi);

					}
					/* alert(response); */
				},
				onFailure : function() {
					alert('Problems on Ajax Request')
				}

			});
}

function GetProductListBySubcategory(category, subcategory, languajeId) {
	// var productsdiv = document.createElement("div");

	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				parameters : {
					method : 'GetProductListBySubcategory',
					category_id : category,
					language_id : languajeId,
					subcategory_id : subcategory
				},
				onSuccess : function(transport) {

					var response = transport.responseXML || "no response text";
					var products = response.getElementsByTagName("product");
					// alert('aca');
					document.getElementById("mainContent").innerHTML="<div id=\"productsContent\"><div id=\"products\"></div></div>";
					var productsDiv = document.getElementById("products");
					productsDiv.innerHTML="";

					for ( var i = 0; i < products.length; i++) {


						var divi = document.createElement("div");
						// var attr = document.createAttibute("id");
						divi.setAttribute("class","productDiv");
						divi.setAttribute("id", "product"
								+ products[i].getAttribute("id"));
						divi.setAttribute("category_id",products[i].getElementsByTagName("category_id")[0].firstChild.nodeValue);
						divi.setAttribute("subcategory_id",products[i].getElementsByTagName("subcategory_id")[0].firstChild.nodeValue);
						divi.setAttribute("sales_rank",products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue);
						divi.setAttribute("price",products[i].getElementsByTagName("price")[0].firstChild.nodeValue);
						var imgDiv = document.createElement("div");
						imgDiv.setAttribute("class", "ProductImg");
						var image = document.createElement("img");
						image.setAttribute("src",products[i].getElementsByTagName("image_url")[0].firstChild.nodeValue);
						imgDiv.appendChild(image);
						var name = document.createElement("div");
						name.setAttribute("class", "ProductName");
						name.innerHTML = "<p>" + products[i]
								.getElementsByTagName("name")[0].firstChild.nodeValue+"</p>";
						name.setAttribute("onclick","GetProduct(" + products[i].getAttribute("id") + ");");
						
						var price = document.createElement("div");
						price.innerHTML = "<p> $ " + divi.getAttribute("price") +"</p>";
						divi.appendChild(name);
						divi.appendChild(price);
						divi.appendChild(imgDiv);
						// alert(products[i].getElementsByTagName("name")[0].firstChild.nodeValue)

						// var subcat = document.createElement("div");
						// divi.appendChild(subcat);
						// setSubcategoryList(divi,i+1,languajeId);
						// divi.setAttribute("onclick","getSubcategoryListAsTable("
						// + (i + 1) +","+ languajeId +")");
						// divi.innerHTML =
						// categories[i].getElementsByTagName("name")[0].firstChild.nodeValue
						// productsDiv.innerHTML = productsDiv.innerHTML + '<p>'
						// +
						// categories[i].getElementsByTagName("name")[0].firstChild.nodeValue
						// + '</p>'
						productsDiv.appendChild(divi);

					}
					/* alert(response); */
				},
				onFailure : function() {
					alert('Problems on Ajax Request')
				}

			});
}

function dvdParser(product){
		
		var productsDiv = document.getElementById("products");
	
		var category = product.getElementsByTagName("category_id")[0].firstChild.nodeValue;
		var subcat = product.getElementsByTagName("subcategory_id")[0].firstChild.nodeValue;
		var name = product.getElementsByTagName("name")[0].firstChild.nodeValue;
		var salesRank = product.getElementsByTagName("sales_rank")[0].firstChild.nodeValue;
		var price = product.getElementsByTagName("price")[0].firstChild.nodeValue;
		var imageUrl = product.getElementsByTagName("image_url")[0].firstChild.nodeValue;
		var actors = product.getElementsByTagName("actors")[0].firstChild.nodeValue;
		var	format = product.getElementsByTagName("format")[0].firstChild.nodeValue;
		var movielanguage = product.getElementsByTagName("language")[0].firstChild.nodeValue;
		var subtitles = product.getElementsByTagName("subtitles")[0].firstChild.nodeValue;
		var region = product.getElementsByTagName("region")[0].firstChild.nodeValue;
		var aspectRatio = product.getElementsByTagName("aspect_ratio")[0].firstChild.nodeValue;
		var	diskNumber = product.getElementsByTagName("number_discs")[0].firstChild.nodeValue;
		var releaseDate = product.getElementsByTagName("release_date")[0].firstChild.nodeValue;
		var runtime = product.getElementsByTagName("run_time")[0].firstChild.nodeValue;
		var asin = product.getElementsByTagName("ASIN")[0].firstChild.nodeValue;
		
		var div = document.createElement("div");
		var divTodo = document.createElement("div");
		divTodo.setAttribute("class","oneProduct");
	
		divTodo.setAttribute("category_id",category);
		divTodo.setAttribute("subcategory_id",subcat);
	
		var namediv = document.createElement("div");
		namediv.setAttribute("class","productName");
		namediv.innerHTML = name;
		divTodo.appendChild(namediv);
		
		var imageDiv = document.createElement("div");
		var image = document.createElement("img");
		image.setAttribute("src",imageUrl);
		imageDiv.appendChild(image);
		divTodo.appendChild(imageDiv);
		
		var pricediv = document.createElement("div");
		pricediv.setAttribute("class","productPrice");
		pricediv.innerHTML = "<h3> Price: </h3>" + "$" + price;
		div.appendChild(pricediv);

		
		var actorsdiv= document.createElement("div");
		actorsdiv.setAttribute("class","actorsName");
		actorsdiv.innerHTML ="<h3> Actors: </h3>" + "<p>" + actors + "</p>";
		div.appendChild(actorsdiv)
		
		
		var formatdiv= document.createElement("div");
		formatdiv.setAttribute("class","formatDiv");
		formatdiv.innerHTML = "<h3> Format: </h3>" +"<p>" + format + "</p>";
		div.appendChild(formatdiv);
		
		
		var movielanguagediv = document.createElement("div");
		movielanguagediv.innerHTML = "<h3> Language: </h3>" +"<p>" + movielanguage + "</p>";
		div.appendChild(movielanguagediv);
		
		var subtitlesdiv = document.createElement("div");
		subtitlesdiv.setAttribute("class","subtitles");
		subtitlesdiv.innerHTML = "<h3> Subtitles: </h3>" +"<p>" + subtitles + "</p>";
		div.appendChild(subtitlesdiv);
		
		
		var regiondiv = document.createElement("div");
		regiondiv.setAttribute("class","subtitles");
		regiondiv.innerHTML = "<h3> Region: </h3>" +"<p>" + region + "</p>";
		div.appendChild(regiondiv);

		var runtimediv = document.createElement("div");
		runtimediv.setAttribute("class","subtitles");
		runtimediv.innerHTML = "<h3> Duration: </h3>" +"<p>" + runtime + " mins " + "</p>";
		div.appendChild(runtimediv);
		
		var aspectRatiodiv = document.createElement("div");
		aspectRatiodiv.setAttribute("class","subtitles");
		aspectRatiodiv.innerHTML = "<h3> Aspect Ratio: </h3>" + "<p>" +aspectRatio + "</p>";
		div.appendChild(aspectRatiodiv);
		
		var diskNumberdiv = document.createElement("div");
		diskNumberdiv.setAttribute("class","subtitles");
		diskNumberdiv.innerHTML = "<h3> Discs Number: </h3>" +"<p>" + diskNumber + "</p>";
		div.appendChild(diskNumberdiv);
		
		var releaseDatediv = document.createElement("div");
		releaseDatediv.setAttribute("class","subtitles");
		releaseDatediv.innerHTML = "<h3> Release Date: </h3>" +"<p>" + releaseDate + "</p>";
		div.appendChild(releaseDatediv);

		var asindiv = document.createElement("div");
		asindiv.setAttribute("class","subtitles");
		asindiv.innerHTML = "<h3> ASIN: </h3>" +"<p>" + asin + "</p>";
		div.appendChild(asindiv);

		var addToOrderButton2 = document.createElement("input");
		var productID = product.getAttribute("id");
		addToOrderButton2.setAttribute("type","button");
		addToOrderButton2.setAttribute("value","Agregar a Orden");
		addToOrderButton2.setAttribute("onclick","displayPopUpOrder("+productID+",\" "+ name + "\");");
		div.appendChild(addToOrderButton2);
		
		var addToOrderButton = document.createElement("div");
		addToOrderButton.innerHTML="<select id=\"quantity\" name=\"quantity\"><option selected=\"\" value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select>";
		div.appendChild(addToOrderButton);
		//	("sales_rank",products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue);
		//divi.setAttribute("price",products[i].getElementsByTagName("price")[0].firstChild.nodeValue);
		//alert(booklanguage);
		
		divTodo.appendChild(div);
		productsDiv.appendChild(divTodo);

	}
function bookParser (product){
	
	var productsDiv = document.getElementById("products");
	
	var category = product.getElementsByTagName("category_id")[0].firstChild.nodeValue;
	var subcat = product.getElementsByTagName("subcategory_id")[0].firstChild.nodeValue;
	var name = product.getElementsByTagName("name")[0].firstChild.nodeValue;
	var salesRank = product.getElementsByTagName("sales_rank")[0].firstChild.nodeValue;
	var price = product.getElementsByTagName("price")[0].firstChild.nodeValue;
	var imageUrl = product.getElementsByTagName("image_url")[0].firstChild.nodeValue;
	var authors = product.getElementsByTagName("authors")[0].firstChild.nodeValue;
	var	publisher = product.getElementsByTagName("publisher")[0].firstChild.nodeValue;
	var published_date = product.getElementsByTagName("published_date")[0].firstChild.nodeValue;
	var ISBN_10 = product.getElementsByTagName("ISBN_10")[0].firstChild.nodeValue;
	var ISBN_13 = product.getElementsByTagName("ISBN_13")[0].firstChild.nodeValue;
	var booklanguage = product.getElementsByTagName("language")[0].firstChild.nodeValue;
	
	var div = document.createElement("div");
	var divTodo = document.createElement("div");
	divTodo.setAttribute("class","oneProduct");
	
	divTodo.setAttribute("category_id",category);
	divTodo.setAttribute("subcategory_id",subcat);
	
	var namediv = document.createElement("div");
	namediv.setAttribute("class","productName");
	namediv.innerHTML = "<p>"+ name +"</p>";
	divTodo.appendChild(namediv);
	
	var imageDiv = document.createElement("div");
	var image = document.createElement("img");
	image.setAttribute("src",imageUrl);
	imageDiv.appendChild(image);
	divTodo.appendChild(imageDiv);
	
	var pricediv = document.createElement("div");
	pricediv.setAttribute("class","productPrice");
	pricediv.innerHTML =  "<h3> Price: </h3>" + "<p>"+  price+"</p>";
	div.appendChild(pricediv);
	
	
	var authorsdiv= document.createElement("div");
	authorsdiv.setAttribute("class","authorName");
	authorsdiv.innerHTML = "<h3> Authors: </h3>" + "<p>" + authors +"</p>";
	div.appendChild(authorsdiv);
	
	var publisherdiv = document.createElement("div");
	publisherdiv.setAttribute("class","publisher");
	publisherdiv.innerHTML = "<h3> Publisher: </h3>" + "<p>"+ publisher+"</p>";
	div.appendChild(publisherdiv);
	
	
	var published_date_div = document.createElement("div");
	//published_date_div.setAttributte("class","publisherdate");
	published_date_div.innerHTML = "<h3> Published Date: </h3>" + "<p>"+ published_date+"</p>";
	div.appendChild(published_date_div);
	
	var ISBN_10div = document.createElement("div");
	//ISBN_10div.setAttributte("class","publisherdate");
	ISBN_10div.innerHTML = "<h3> ISBN 10: </h3>" + "<p>"+ ISBN_10+"</p>";
	div.appendChild(ISBN_10div);
	
	var ISBN_13div = document.createElement("div");
	//ISBN_13div.setAttributte("class","publisherdate");
	ISBN_13div.innerHTML = "<h3> ISBN 13: </h3>" + "<p>"+ ISBN_13+"</p>";
	div.appendChild(ISBN_13div);
	
	
	var booklanguagediv = document.createElement("div");
	booklanguagediv.innerHTML = "<h3> Book Language </h3>" + "<p>"+ booklanguage+"</p>";
	div.appendChild(booklanguagediv);
	
	/*var addToOrderButton2 = document.createElement("div");
	var productID = product.getAttribute("id");
	addToOrderButton2.innerHTML="<input type=\"button\" onclick=\"addOrderItem("+productID+",qtySelected())\" value=\"Agregar a Orden\" />";
	div.appendChild(addToOrderButton2);*/
	var addToOrderButton2 = document.createElement("input");
	var productID = product.getAttribute("id");
	addToOrderButton2.setAttribute("type","button");
	addToOrderButton2.setAttribute("value","Agregar a Orden");
	addToOrderButton2.setAttribute("onclick","displayPopUpOrder("+productID+",\" "+ name + "\");");

	div.appendChild(addToOrderButton2);
	
	var addToOrderButton = document.createElement("div");
	
	addToOrderButton.innerHTML="<select id=\"quantity\" name=\"quantity\"><option selected=\"\" value=\"1\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select>";
	div.appendChild(addToOrderButton);
	
	divTodo.appendChild(div);

	
	//	("sales_rank",products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue);
	//divi.setAttribute("price",products[i].getElementsByTagName("price")[0].firstChild.nodeValue);
	//alert(booklanguage);
	
	productsDiv.appendChild(divTodo);
}
function qtySelected(){
	return document.getElementById("quantity").value;
}
function GetProduct(productId) {
	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				parameters : {
					method : 'GetProduct',
					product_id : productId,
				},
				onSuccess : function(transport) {

					var response = transport.responseXML || "no response text";
					var product = response.getElementsByTagName("product")[0];
					document.getElementById("mainContent").innerHTML="<div id=\"productsContent\"><div id=\"products\"></div></div>";
					var productsDiv = document.getElementById("products");
					productsDiv.innerHTML="";

					if(product.getElementsByTagName("category_id")[0].firstChild.nodeValue == 1){
						//alert("hola");
						dvdParser(product);
					}else{
						bookParser(product);
					}
					//....
					/* alert(response); */
				},
				onFailure : function() {
					alert('Problems on Ajax Request')
				}

			});
}
function searchProductByName(){
	document.getElementById("mainContent").innerHTML='<img src="imagenes/ajax-loader.gif" alt="loading"/>';
	var criterio=document.getElementById('srch_fld').value;
	
	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				parameters : {
					method : 'GetProductListByName',
					criteria : criterio,
				},
				onSuccess : function(transport) {

					var response = transport.responseXML || "no response text";
					var products = response.getElementsByTagName("product");
					document.getElementById("mainContent").innerHTML="<div id=\"productsContent\"><div id=\"products\"></div></div>";
					var productsDiv = document.getElementById("products");
					
					if( products.length == 0){
							displayAlert("Su Busqueda no produjo ningun resultado");
							return;
					}
					productsDiv.innerHTML="";
					if( products.length == 1){
						var product=GetProduct(products[0].getAttribute("id"));
						if(products[0].getElementsByTagName("category_id")[0].firstChild.nodeValue == 1){
							dvdParser(product);
						}else{
							bookParser(poduct);
						}
						return;
					}else{
						for(var i=0 ; i < products.length; i++){
							var divi = document.createElement("div");
							// var attr = document.createAttibute("id");
							divi.setAttribute("class","productDiv");
							divi.setAttribute("id", "product"+ products[i].getAttribute("id"));
							divi.setAttribute("category_id",products[i].getElementsByTagName("category_id")[0].firstChild.nodeValue);
							divi.setAttribute("subcategory_id",products[i].getElementsByTagName("subcategory_id")[0].firstChild.nodeValue);
							divi.setAttribute("sales_rank",products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue);
							divi.setAttribute("price",products[i].getElementsByTagName("price")[0].firstChild.nodeValue);
							var imgDiv = document.createElement("div");
							imgDiv.setAttribute("class", "ProductImg");
							var image = document.createElement("img");
							image.setAttribute("src",products[i].getElementsByTagName("image_url")[0].firstChild.nodeValue);
							imgDiv.appendChild(image);
							var name = document.createElement("div");
							name.setAttribute("class", "ProductName");
							name.innerHTML = "<p>" + products[i]
									.getElementsByTagName("name")[0].firstChild.nodeValue+"</p>";
							name.setAttribute("onclick","GetProduct(" + products[i].getAttribute("id") + ");");
							var price = document.createElement("div");
							price.innerHTML = "<p> $ " + divi.getAttribute("price") +"</p>";
		
							divi.appendChild(name);
							divi.appendChild(price);
							divi.appendChild(imgDiv);
							productsDiv.appendChild(divi);
						}
					}
				},
				onFailure : function() {
					alert('Problems on Ajax Request')
				}

			});
}

function DisplayHotest6DVD(){
	DisplayHotest6(1, 1,false);
	
	DisplayHotest6(2, 1,true);
	
}

function DisplayHotest6(category, languajeId,appendFlag) {
		// var productsdiv = document.createElement("div");
//	category='1';
	languajeId='1';
	var req = new Ajax.Request(
			urlcatalog,
			{
				method : 'get',
				//asynchronous:false,
				parameters : {
					method : 'GetProductListByCategory',
					category_id : category,
					language_id : languajeId
				},
				onSuccess : function(transport) {

					var response = transport.responseXML || "no response text";
					var products = response.getElementsByTagName("product");
					// alert('aca');
					if(!appendFlag){
						document.getElementById("mainContent").innerHTML="<div id=\"productsContent\"><div id=\"products\"></div></div>";
					}
					var productsDiv = document.getElementById("products");
					if(!appendFlag){
						productsDiv.innerHTML="";
					}
					var salesRankArray = new Array(6);
					var ElementsArray = new Array(6);
					for(var i = 0; i< 6; i++){
						salesRankArray[i]=0;
					}
					
					for ( var i = 0; i < products.length; i++) {
						var divi = document.createElement("div");
						// var attr = document.createAttibute("id");
						divi.setAttribute("class","productDiv");
						divi.setAttribute("id", "product"+ products[i].getAttribute("id"));
						divi.setAttribute("category_id",products[i].getElementsByTagName("category_id")[0].firstChild.nodeValue);
						divi.setAttribute("subcategory_id",products[i].getElementsByTagName("subcategory_id")[0].firstChild.nodeValue);
						divi.setAttribute("sales_rank",products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue);
						var SR = products[i].getElementsByTagName("sales_rank")[0].firstChild.nodeValue;
						divi.setAttribute("price",products[i].getElementsByTagName("price")[0].firstChild.nodeValue);
						var imgDiv = document.createElement("div");
						imgDiv.setAttribute("class", "ProductImg");
						var image = document.createElement("img");
						image.setAttribute("src",products[i].getElementsByTagName("image_url")[0].firstChild.nodeValue);
						imgDiv.appendChild(image);
						var name = document.createElement("div");
						name.setAttribute("class", "ProductName");
						name.innerHTML = "<p>" + products[i]
								.getElementsByTagName("name")[0].firstChild.nodeValue+"</p>";
						name.setAttribute("onclick","GetProduct(" + products[i].getAttribute("id") + ");");
						var price = document.createElement("div");
						price.innerHTML = "<p> $ " + divi.getAttribute("price") +"</p>";
	
						divi.appendChild(name);
						divi.appendChild(price);
						divi.appendChild(imgDiv);
						
						for(var j = 0; j < 6 ; j++){
							//alert(sales_rank);
							var a=(Number(SR));
							if( salesRankArray[j] <= a){
								salesRankArray[j] = a;
								ElementsArray[j] = divi;
								break;
							}
						}
					}
					for(var i = 0; i<6; i++){
						productsDiv.appendChild(ElementsArray[i]);
						//alert(salesRankArray[i]);
					}
					/* alert(response); */
				},
				onFailure : function() {
					alert('Problems on Ajax Request')
				}

			});
}

$j(document).ready(function(){
	getCategory('1');
	/*DisplayHotest6DVD();*/
});